﻿using System.Data;
using U8DllPack.Controller;
using U8DllPack.Model;
using U8DllPack.Properties;

namespace U8DllPack.View
{
    public partial class FrmDllfiles : Form
    {

        public DataTable? DllPackDT { get; private set; }

        public FrmDllfiles()
        {
            InitializeComponent();
            Icon = Resources.pack_32;
        }

        private void FrmDllfiles_Load(object sender, EventArgs e)
        {
            dgvList.AutoGenerateColumns = false;
            DllPackDT = CustomDataTable.ReturnDPDT();
            Sub_Load();
        }

        private void Sub_Load()
        {
            DataTable dt = CustomDataTable.DT_U8ver();
            dt.Rows[0]["Text"] = "请选择 ...";
            cbbU8ver.DataSource = dt;
            cbbU8ver.DisplayMember = "Text";
            cbbU8ver.ValueMember = "Id";
            cbbU8ver.SelectedIndex = 0;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            Sub_Select();
        }

        private void Sub_Select()
        {
            OpenFileDialog ofd = new()
            {
                Filter = "(*.dll链接文件)|*.dll|(*.*所有文件)|*.*",
                Title = "选择U8运行链接文件 ...",
                Multiselect = true
            };
            ofd.ShowDialog();
            string[] files = ofd.FileNames;
            if (files.Length == 0) return;
            DllPackDT ??= CustomDataTable.ReturnDPDT();

            foreach (string file in files)
            {
                DataRow row = DllPackDT.NewRow();
                row["filepath"] = file;
                string filename = Path.GetFileName(file);
                DataRow[] drs = DllPackDT.Select("name='" + filename + "'");
                if (drs.Length != 0) continue;
                row["name"] = filename;
                File_VersionInfo? fv = BaseCls.GetVersions(file);
                if (fv == null)
                {
                    DllPackDT.Rows.Add(row);
                }
                else
                {
                    row["ver"] = fv.FileVersion.Trim_Ext();
                    if (fv.FileVersion.IsNullOrWhiteSpace())
                    {
                        row["ver"] = fv.ProductVersion.Trim_Ext();
                    }
                    row["desc"] = fv.FileDescription.Trim_Ext();
                    DllPackDT.Rows.Add(row);
                }
            }
            dgvList.DataSource = DllPackDT;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            panMain.Enabled = false;
            Sub_Del();
            panMain.Enabled = true;
        }

        private void Sub_Del()
        {
            bool flag = dgvList.SelectedRows.Count == 0;
            if (!flag)
            {
                foreach (object obj in dgvList.SelectedRows)
                {
                    DataGridViewRow row = (DataGridViewRow)obj;
                    dgvList.Rows.Remove(row);
                }
            }
        }

        private void btnEmpty_Click(object sender, EventArgs e)
        {
            panMain.Enabled = false;
            Sub_Empty();
            panMain.Enabled = true;
        }

        private void Sub_Empty()
        {
            if (dgvList.SelectedRows.Count == 0) return;
            DataTable? dt = dgvList.DataSource as DataTable;
            if (dt == null || dt.Rows.Count == 0) return;
            dt.Rows.Clear();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            panMain.Enabled = false;
            Sub_Submit();
            panMain.Enabled = true;
        }

        private void Sub_Submit()
        {
            if (cbbU8ver.SelectedIndex <= 0)
            {
                CommFun.MsgBox("请选择U8版本！", 1);
                cbbU8ver.DroppedDown = true;
                return;
            }
            byte? U8ver = cbbU8ver.SelectedValue as byte?;
            string? Respath = txtRespath.Text.Trim_Ext();
            if (U8ver.HasValue && U8ver>0 && Respath.IsNullOrWhiteSpace())
            {
                CommFun.MsgBox("请输入链接文件存放的子目录！", 1);
                txtRespath.Focus_Ext();
                return;
            }
            if (U8ver == 0) Respath = null;
            if (dgvList.Rows.Count <= 0)
            {
                CommFun.MsgBox("请选择链接文件！", 1);
                btnSelect.Focus_Ext();
                return;
            }
            if (DllPackDT == null || DllPackDT.Rows.Count <= 0)
            {
                CommFun.MsgBox("内存表获取错误！", 1);
                btnSelect.Focus_Ext();
                return;
            }
            foreach (DataRow row in DllPackDT.Rows)
            {
                row["u8ver"] = U8ver;
                row["respath"] = Respath;
            }
            DialogResult = DialogResult.OK;
        }

        private void cbbU8ver_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ComboBox cbb = (ComboBox)sender;
            byte? ver = cbb.SelectedValue as byte?;
            txtRespath.Enabled = true;
            if (ver.HasValue)
            {
                if (ver == 0)
                {
                    txtRespath.Enabled = false;
                }
                else
                {
                    txtRespath.Text = cbb.Text;
                }
            }
        }

        private void txtRespath_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            char[] chars =
            [
                '\\',
                '/',
                ':',
                '*',
                '?',
                '"',
                '<',
                '>',
                '|'
            ];
            ToolTip tip = new()
            {
                IsBalloon = true,
                ToolTipIcon = ToolTipIcon.Warning,
                ToolTipTitle = "名称不能包含以下字符"
            };
            foreach (char c in chars)
            {
                if (c == e.KeyChar)
                {
                    e.Handled = true;
                    tip.Show("\\/:*?\"<>|", txt, 0, -(txt.Top + txt.Height + txt.Height));
                    return;
                }
            }
            tip.Dispose();
        }
    }
}
